package cpcn.dsp.demo.tx.score;

import cpcn.dsp.demo.consts.DSPConsts;
import cpcn.dsp.demo.util.HttpsUtil;
import cpcn.dsp.institution.api.security.EncryptAndDecrypt;
import cpcn.dsp.institution.api.system.DSPInstitutionEnvironment;
import cpcn.dsp.institution.api.tx.score.ScoreTx5153Request;
import cpcn.dsp.institution.api.tx.score.ScoreTx5153Response;
import cpcn.dsp.institution.api.util.Base64;
import cpcn.dsp.institution.api.util.GUIDGenerator;

import java.util.HashMap;
import java.util.Map;

public class Test5153 {
    public static void main(String[] args) throws Exception {

        //1.初始化
        DSPInstitutionEnvironment.initialize(DSPConsts.Keystore_test, DSPConsts.myKeystorePassword_test, DSPConsts.publicKey_test, DSPConsts.MSG_ENCRYPT_20);
        //1.1 encryptSN、signSN值不为空，说明初始化证书成功
        System.out.println(DSPInstitutionEnvironment.encryptSN);
        System.out.println(DSPInstitutionEnvironment.signSN);
        //2.生成请求明文
        ScoreTx5153Request tx5153Request = new ScoreTx5153Request();
        tx5153Request.setInstitutionID("");// 机构号由中金分配
        tx5153Request.setTxSN(GUIDGenerator.genGUID());
        tx5153Request.setEncryptType("MD5");//SHA256/MD5/SM3
        tx5153Request.setIdentificationNumber("110101198001010037");
        tx5153Request.setRemark("");//仅测试环境判断该字段1->未查询到结果,2->未知,其他->一致

        //报文处理
        tx5153Request.process();
        String plainText = tx5153Request.getRequestPlainText();
        System.out.println("请求明文：" + plainText);

        String reqMessage = tx5153Request.getRequestMessage();
        String reqSignature = tx5153Request.getRequestSignature();
        String requestDgtlEnvlp = tx5153Request.getRequestDgtlEnvlp();
        String requestSignSN = tx5153Request.getRequestSignSN();
        String requestEncryptSN = tx5153Request.getRequestEncryptSN();
        //生成参数
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("message", reqMessage);
        paramMap.put("signature", reqSignature);
        paramMap.put("dgtlenvlp", requestDgtlEnvlp);
        paramMap.put("signSN", requestSignSN);
        paramMap.put("encryptSN", requestEncryptSN);
        //生成请求参数
        String requestString = EncryptAndDecrypt.createLinkStringByGet(paramMap);
        System.out.println("请求密文：" + requestString);
        //请求接口
        String result = HttpsUtil.post(DSPConsts.SCORE_URL_TEST, requestString, null);
        System.out.println("响应密文：" + result);

        String respPlainText = null;
        String[] respMsg = result.split(",");
        if (respMsg.length == 1 && -1 == respMsg[0].indexOf(',')) {
            respPlainText = Base64.decode(respMsg[0], "UTF-8");
        } else if (respMsg.length == 5) {
            String responseMessage = respMsg[0];
            String responseSignature = respMsg[1];
            String responseDgtlEnvlp = respMsg[2];
            String respSignSN = respMsg[3];
            String respEncryptSN = respMsg[4];

            ScoreTx5153Response tx5153Response = new ScoreTx5153Response(responseMessage, responseSignature, responseDgtlEnvlp, respSignSN, respEncryptSN);
            respPlainText = tx5153Response.getResponsePlainText();
            if ("2000".equals(tx5153Response.getCode())) {

                //System.out.println("验证结果" + verification);
            }
        }
        System.out.println("响应明文：" + respPlainText);

    }
}
